//
//  ForgetPassWordVC.m
//  PetAPP
//
//  Created by apple on 16/3/4.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "ForgetPassWordVC.h"
#import "LoginTextField.h"
#import "CheckTextField.h"
#import <CommonCrypto/CommonDigest.h>
#import "NetworkingHandle.h"


#define WIDTH self.view.frame.size.width

@interface ForgetPassWordVC () <UITextFieldDelegate>

@property (nonatomic, retain) LoginTextField *textFileOfName;

@property (nonatomic, retain) CheckTextField *textFileOfMessage;

@property (nonatomic, retain) LoginTextField *textFileOfPassword;

@property (nonatomic, retain) UIButton *buttonOfComplete;

@end

@implementation ForgetPassWordVC

- (void)dealloc {

    [_textFileOfName release];
    [_textFileOfPassword release];
    [_textFileOfMessage release];
    [_buttonOfComplete release];
    [super dealloc];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createSubViews];
    if (_flag) self.navigationItem.title = @"忘记密码";
    else    self.navigationItem.title = @"注册";
    
}

- (void)createSubViews {

    self.textFileOfName = [[LoginTextField alloc] initWithFrame:CGRectMake(20, 60 + 64, WIDTH - 40, 30)];
    [self.view addSubview:_textFileOfName];
    [_textFileOfName release];
    self.textFileOfName.placeholder = @"手机号";
    
    
    self.textFileOfMessage = [[CheckTextField alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.textFileOfName.frame) + 30, WIDTH - 40, 30)];
    [self.view addSubview:_textFileOfMessage];
    [_textFileOfMessage release];
    self.textFileOfMessage.placeholder = @"验证码";
    
    UILabel *labelOfGetMessage = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.textFileOfMessage.frame) - 95, CGRectGetMaxY(self.textFileOfName.frame) + 30, 90, 30)];
    [self.view addSubview:labelOfGetMessage];
    [labelOfGetMessage release];
    labelOfGetMessage.text = @"获取验证码";
    labelOfGetMessage.textColor = [UIColor grayColor];
    labelOfGetMessage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction:)];
    [labelOfGetMessage addGestureRecognizer:tap];
    [tap release];
    

    self.textFileOfPassword = [[LoginTextField alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.textFileOfMessage.frame) + 30, WIDTH - 40, 30)];
    [self.view addSubview:_textFileOfPassword];
    [_textFileOfPassword release];
    self.textFileOfPassword.placeholder = @"密码";
    self.textFileOfPassword.secureTextEntry = YES;
    self.textFileOfPassword.delegate = self;
    [self.textFileOfPassword addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    
    
    UILabel *labelOfSoundMessage = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.textFileOfPassword.frame) + 10, CGRectGetMaxX(self.textFileOfMessage.frame) - 40, 20)];
    [self.view addSubview:labelOfSoundMessage];
    [labelOfSoundMessage release];
    labelOfSoundMessage.font = [UIFont systemFontOfSize:15];
    labelOfSoundMessage.textColor = [UIColor grayColor];
    /** 设置通过搜索设置可变字符串 */
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:@"短信验证码收不到?尝试下语音验证码吧"];
    [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255/256.0 green:207/256.0 blue:87/256.0 alpha:1] range:[@"短信验证码收不到?尝试下语音验证码吧" rangeOfString:@"语音验证码"]];
    [labelOfSoundMessage setAttributedText:attribute];
    [attribute release];
    
    
    
    self.buttonOfComplete = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_buttonOfComplete];
    self.buttonOfComplete.frame = CGRectMake(20, CGRectGetMaxY(labelOfSoundMessage.frame) + 40, WIDTH - 40, 40);
    self.buttonOfComplete.backgroundColor = [UIColor colorWithRed:0.7721 green:0.7721 blue:0.7721 alpha:1.0];
    [self.buttonOfComplete setTitle:@"完成" forState:UIControlStateNormal];
    self.buttonOfComplete.layer.cornerRadius = 5;
    [self.buttonOfComplete setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.buttonOfComplete.userInteractionEnabled = NO;
    [self.buttonOfComplete addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *labelOfHelp = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.buttonOfComplete.frame) + 10, CGRectGetMaxX(self.buttonOfComplete.frame) - 40, 40)];
    [self.view addSubview:labelOfHelp];
    [labelOfHelp release];
    labelOfHelp.text = @"注: 如在找回密码过程中遇到任何问题，请拨打客服电话:400-040-7278";
    labelOfHelp.textAlignment = NSTextAlignmentCenter;
    labelOfHelp.numberOfLines = 2;
    labelOfHelp.font = [UIFont systemFontOfSize:15];
    labelOfHelp.textColor = [UIColor grayColor];

}

- (void)textFieldDidChange:(UITextField *)textField {
    if (textField.text.length > 3 && textField == self.textFileOfPassword){
        self.buttonOfComplete.backgroundColor = [UIColor colorWithRed:255/256.0 green:207/256.0 blue:87/256.0 alpha:1];
        self.buttonOfComplete.userInteractionEnabled = YES;
    }else {
        self.buttonOfComplete.backgroundColor = [UIColor colorWithRed:0.7721 green:0.7721 blue:0.7721 alpha:1.0];
        self.buttonOfComplete.userInteractionEnabled = NO;
    }
    
    
}

- (void)handleButtonAction:(UIButton *)button {
    
    if (_flag) {
        /** 更改body中的账号和密码传值 */
        NSString *body = [NSString stringWithFormat:@"UUID=4D8F8B91-5DBA-493C-BD9C-DF19D79DC566&codeSMS=%@&idfa=177896AB-D8CF-4150-99C2-8E562731F6F7&password=%@&phoneNumber=%@&service=findpasswordbyphone&sign=c1bc57e151fd480eb20ac5e266bec308&version=d1bd83a33f1a841ab7fda32449746cc4", self.textFileOfMessage.text, self.textFileOfPassword.text, self.textFileOfName.text];
        [NetworkingHandle handlePassWord:@"http://bbs.chinapet.com/plugin.php?id=leepet_thread:api&action=findpasswordbyphone" AndBody:body value:@{@"Content-Type" : @"application/x-www-form-urlencoded"} handleBlock:^(id result) {
            NSLog(@"%@", result);
        }];
    }else {
        /** 更改body中的账号和密码传值 */
        NSString *body = [NSString stringWithFormat:@"UUID=4D8F8B91-5DBA-493C-BD9C-DF19D79DC566&codeSMS=%@&flag=1&idfa=&password=%@&phoneNumber=%@&service=phoneregister&sign=c1bc57e151fd480eb20ac5e266bec308&version=d1bd83a33f1a841ab7fda32449746cc4", self.textFileOfMessage.text, self.textFileOfPassword.text, self.textFileOfName.text];
        [NetworkingHandle handlePassWord:@"http://bbs.chinapet.com/plugin.php?id=leepet_thread:api&action=phoneregister" AndBody:body value:@{@"Content-Type" : @"application/x-www-form-urlencoded"} handleBlock:^(id result) {
            NSLog(@"%@", result);
        }];
    }
}

- (void)handleTapAction:(UITapGestureRecognizer *)tap {
    
    NSString *body = [NSString stringWithFormat:@"phoneNumber=%@&service=sendsmsmessage&smsType=%d&version=d1bd83a33f1a841ab7fda32449746cc4&voiceSMS=0", self.textFileOfName.text, _flag];
    [NetworkingHandle handlePassWord:@"http://bbs.chinapet.com/plugin.php?id=leepet_thread:api&action=sendsmsmessage" AndBody:body value:@{@"Content-Type" : @"application/x-www-form-urlencoded"} handleBlock:^(id result) {
        NSLog(@"%@", result);
    }];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.view endEditing:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
